package test0ne.多线程.test;

import test0ne.多线程.SleepUtils;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchTest1 {
    private static final int STUDENT_COUNT = 50;

    private static CountDownLatch countDownLatch = new CountDownLatch(STUDENT_COUNT);

    public static void main(String[] args) {
        Thread teacher = new Thread(new Teacher());
        teacher.start();
        SleepUtils.second(1);
        for (int i = 0; i < STUDENT_COUNT; i++) {
            Thread stu = new Thread(new Student());
            stu.start();
        }
    }

    static class Teacher implements Runnable{

        @Override
        public void run() {

            System.out.println("老师"+Thread.currentThread().getName()+"来了，等所有同学都到了才上课...");
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("所有同学都到齐了，开始上课...");
        }

    }



    static class Student implements Runnable{
        @Override
        public void run() {
            System.out.println("同学"+Thread.currentThread().getName()+":进入了教室...");
            countDownLatch.countDown();
        }
    }
}
